home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / textmap.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  272 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    textmap - 
  19.  *        Use texture mapping to draw text.
  20.  *
  21.  *                Paul Haeberli - 1991
  22.  *
  23.  */
  24. #include "gl.h"
  25. #include "image.h"
  26. #include "textmap.h"
  27.  
  28. static texfnt *curtfnt;
  29. static unsigned char *fb;
  30. static unsigned char *gptr;
  31.  
  32. /*
  33.  *    get metric data into image
  34.  *
  35.  */
  36. static initget()
  37. {
  38.     gptr = fb;
  39. }
  40.  
  41. static getbytes(buf,n)
  42. unsigned char *buf;
  43. int n;
  44. {
  45.     while(n--) 
  46.      *buf++ = *gptr++;
  47. }
  48.  
  49. static fixrow(sptr,n)
  50. unsigned short *sptr;
  51. int n;
  52. {
  53.     while(n--) {
  54.     *sptr = *sptr + (0xff<<8);
  55.     *sptr = (*sptr<<8) + 0xff;
  56.     sptr++;
  57.     }
  58. }
  59.  
  60. texfnt *readtexfont(name)
  61. char *name;
  62. {
  63.     texfnt *tfnt;
  64.     IMAGE *image;
  65.     unsigned char *cptr;
  66.     unsigned short *sbuf, *sptr;
  67.     short advancecell, xadvance;
  68.     short llx, lly, urx, ury, ox, oy;
  69.     int i, y, c, extralines;
  70.     texchardesc *cd;
  71.  
  72.     tfnt = (texfnt *)malloc(sizeof(texfnt));
  73.     image = iopen(name,"r");
  74.     if(!image) {
  75.      fprintf(stderr,"textmap: can't open font image %s\n",name);
  76.      exit(1);
  77.     }
  78.     extralines = image->ysize-image->xsize;
  79.     if(extralines<1) {
  80.      fprintf(stderr,"textmap: bad input font!!\n");
  81.      exit(1);
  82.     }
  83.     fb = (unsigned char *)malloc(image->xsize*extralines);
  84.     sbuf = (unsigned short *)malloc(image->xsize*sizeof(short));
  85.     cptr = fb;
  86.     for(y=image->xsize; y<image->ysize; y++) {
  87.     getrow(image,sbuf,y,0);
  88.     stoc(sbuf,cptr,image->xsize);
  89.     cptr += image->xsize;
  90.     }
  91.     initget();
  92.     tfnt->rasxsize = image->xsize;
  93.     tfnt->rasysize = image->xsize;
  94.     getbytes(&tfnt->charmin,sizeof(short));
  95.     getbytes(&tfnt->charmax,sizeof(short));
  96.     getbytes(&tfnt->pixhigh,sizeof(float));
  97.     getbytes(&advancecell,sizeof(short));
  98.     tfnt->nchars = tfnt->charmax-tfnt->charmin+1;
  99.     tfnt->chars = (texchardesc *)malloc(tfnt->nchars*sizeof(texchardesc));
  100.     tfnt->rasdata = (unsigned short *)malloc(tfnt->rasxsize*tfnt->rasysize*sizeof(long));
  101.     sptr = tfnt->rasdata;
  102.     for(y=0; y<tfnt->rasysize; y++) {
  103.     getrow(image,sptr,y,0);
  104.     fixrow(sptr,tfnt->rasxsize);
  105.     sptr += tfnt->rasxsize;
  106.     }
  107.     iclose(image);
  108.  
  109.     cd = tfnt->chars;
  110.     for(i=0; i<tfnt->nchars; i++) {
  111.     c = tfnt->charmin+i;
  112.     getbytes(&xadvance,sizeof(short));
  113.     getbytes(&llx,sizeof(short));
  114.     getbytes(&lly,sizeof(short));
  115.     getbytes(&urx,sizeof(short));
  116.     getbytes(&ury,sizeof(short));
  117.     getbytes(&ox,sizeof(short));
  118.     getbytes(&oy,sizeof(short));
  119.     cd->movex = xadvance/(float)advancecell;
  120.  
  121.     if(llx>=0) {
  122.         cd->haveimage = 1;
  123.         cd->llx = (llx-ox)/tfnt->pixhigh;
  124.         cd->lly = (lly-oy)/tfnt->pixhigh;
  125.         cd->urx = (urx-ox+1)/tfnt->pixhigh;
  126.         cd->ury = (ury-oy+1)/tfnt->pixhigh;
  127.         cd->tllx = llx/(float)tfnt->rasxsize;
  128.         cd->tlly = lly/(float)tfnt->rasysize;
  129.         cd->turx = (urx+1)/(float)tfnt->rasxsize;
  130.         cd->tury = (ury+1)/(float)tfnt->rasysize;
  131.         cd->data[0] = cd->tllx;
  132.         cd->data[1] = cd->tlly;
  133.  
  134.         cd->data[2] = cd->llx;
  135.         cd->data[3] = cd->lly;
  136.  
  137.         cd->data[4] = cd->turx;
  138.         cd->data[5] = cd->tlly;
  139.  
  140.         cd->data[6] = cd->urx;
  141.         cd->data[7] = cd->lly;
  142.  
  143.         cd->data[8] = cd->turx;
  144.         cd->data[9] = cd->tury;
  145.  
  146.         cd->data[10] = cd->urx;
  147.         cd->data[11] = cd->ury;
  148.  
  149.         cd->data[12] = cd->tllx;
  150.         cd->data[13] = cd->tury;
  151.  
  152.         cd->data[14] = cd->llx;
  153.         cd->data[15] = cd->ury;
  154.  
  155.         cd->data[16] = cd->llx;
  156.         cd->data[17] = cd->lly;
  157.         cd->data[18] = cd->urx;
  158.         cd->data[19] = cd->lly;
  159.  
  160.         cd->data[20] = cd->urx;
  161.         cd->data[21] = cd->ury;
  162.         cd->data[22] = cd->llx;
  163.         cd->data[23] = cd->ury;
  164.  
  165.     } else {
  166.         cd->haveimage = 0;
  167.     }
  168.     cd++;
  169.     }
  170.     free(fb);
  171.     free(sbuf);
  172.     return tfnt;
  173. }
  174.  
  175. static float tmprops[] = {
  176.     TX_MINFILTER,
  177.     TX_BILINEAR,
  178.     TX_MAGFILTER,
  179.     TX_BILINEAR,
  180.     TX_NULL,
  181. };
  182.  
  183. static float tvprops[] = {
  184.     TV_MODULATE,
  185.     TV_NULL,
  186. };
  187.  
  188. texfont(tfnt)
  189. texfnt *tfnt;
  190. {
  191.     texdef2d(1,2,tfnt->rasxsize,tfnt->rasysize,
  192.      (unsigned long *)tfnt->rasdata,5,tmprops);
  193.     tevdef(1,2,tvprops);
  194.     texbind(0,1);
  195.     tevbind(0,1);
  196.     curtfnt = tfnt;
  197. }
  198.  
  199. float texstrwidth(str)
  200. char *str;
  201. {
  202.     unsigned int c;
  203.     int charmin, tnchars;
  204.     texfnt *tfnt;
  205.     texchardesc *cdbase, *cd;
  206.     float xpos;
  207.  
  208.     tfnt = curtfnt;
  209.     if(!tfnt) {
  210.     fprintf(stderr,"texcharstr: no texfont set!!\n");
  211.     return;
  212.     }
  213.     charmin = tfnt->charmin;
  214.     tnchars = tfnt->nchars;
  215.     cdbase = tfnt->chars;
  216.     xpos = 0.0;
  217.     while(*str) {
  218.     c = *str-charmin;
  219.     if(c<tnchars) {
  220.         cd = cdbase+c;
  221.         xpos += cd->movex;
  222.     }
  223.     str++;
  224.     }
  225.     return xpos;
  226. }
  227.  
  228. texcharstr(str)
  229. unsigned char *str;
  230. {
  231.     unsigned int c;
  232.     int charmin, tnchars;
  233.     texfnt *tfnt;
  234.     texchardesc *cdbase, *cd;
  235.     float *fdata, xpos;
  236.  
  237.     tfnt = curtfnt;
  238.     if(!tfnt) {
  239.     fprintf(stderr,"texcharstr: no texfont set!!\n");
  240.     return;
  241.     }
  242.     charmin = tfnt->charmin;
  243.     tnchars = tfnt->nchars;
  244.     cdbase = tfnt->chars;
  245.     xpos = 0.0;
  246.     while(*str) {
  247.     c = *str-charmin;
  248.     if(c<tnchars) {
  249.         cd = cdbase+c;
  250.         if(cd->haveimage) {
  251.         fdata = cd->data;
  252.         fdata[16] = fdata[2]+xpos;
  253.         fdata[18] = fdata[6]+xpos;
  254.         fdata[20] = fdata[10]+xpos;
  255.         fdata[22] = fdata[14]+xpos;
  256.         bgnpolygon();
  257.             t2f(&fdata[0]);
  258.             v2f(&fdata[16]);
  259.             t2f(&fdata[4]);
  260.             v2f(&fdata[18]);
  261.             t2f(&fdata[8]);
  262.             v2f(&fdata[20]);
  263.             t2f(&fdata[12]);
  264.             v2f(&fdata[22]);
  265.         endpolygon();
  266.         }
  267.         xpos += cd->movex;
  268.     }
  269.     str++;
  270.     }
  271. }
  272.